package com.web;

import com.web.annotation.RequestBody;
import com.web.annotation.ResponseBody;

import java.lang.reflect.Method;

/**
 * @author chengjy
 * @date 2025/4/15
 */
public class WebHandler {

    // 控制器对象
    private final Object objectBean;

    // 处理器方法
    private final Method method;

    private final MethodType methodType;

    public WebHandler(Object objectBean, Method method) {
        this.objectBean = objectBean;
        this.method = method;
        this.methodType = analysisResultType(objectBean ,method);

    }

    private MethodType analysisResultType(Object controllerBean, Method method) {
        if (method.getClass().isAnnotationPresent(ResponseBody.class)) {
            return MethodType.JSON;
        }
        if (method.getReturnType().equals(ModelAndView.class)) {
            return MethodType.LOCAL;
        }
        if (controllerBean.getClass().isAnnotationPresent(ResponseBody.class)) {
            return MethodType.JSON;
        }
        return MethodType.HTML;
    }

    enum MethodType {
        JSON, HTML , LOCAL
    }

    public Object getObjectBean() {
        return objectBean;
    }

    public Method getMethod() {
        return method;
    }

    public MethodType getMethodType() {
        return methodType;
    }
}
